package com.sky.local_ads;

import java.util.List;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.sky.local_ads.activity.StoreOffersListActivity;
import com.sky.local_ads.constants.Constants;
import com.sky.local_ads.fragment.BusinessListFragment;
import com.sky.local_ads.fragment.BusinessMapFragment;
import com.sky.local_ads.listener.ServiceResponseListener;
import com.sky.local_ads.servicemanager.ServiceManager;
import com.sky.local_ads.store_list.StoreDetails;
import com.sky.local_ads.store_list.StoreResponse;
import com.sky.local_ads.store_offers.StoreOfferResponse;
import com.sky.local_ads.store_offers.StoreOffersList;
import com.sky.local_ads.utils.Utils;
import com.sky.local_ads.widget.TOProgressDialog;
import com.sky.trueoffers.R;



public class BusinessListActivity extends FragmentActivity implements OnClickListener {
	private BusinessListFragment businessListFragment;
	private BusinessMapFragment businessMapFragment;
	private TOProgressDialog progressDialog;
	private boolean isService=false,isMapView=false;
	private RelativeLayout storeListLayout,storeMapLayout;
	private Button storeViewButton;
	private boolean isPlayServiceAvailable=false;
	private int playServiceStatus=-1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_store_list);
		businessListFragment=new BusinessListFragment();
		businessMapFragment=new BusinessMapFragment();
		initElements();
		initListFragment();
		playServiceStatus = Utils
				.getPlayServiceStatus(this);
		if ( playServiceStatus == ConnectionResult.SUCCESS ) {
			isPlayServiceAvailable=true;
		}
		
		if( isPlayServiceAvailable ){
			initMapFragment();
		}
		
		
		if(!isService){
			isService=true;
			progressDialog=new TOProgressDialog(this, "hi", "message", false, false);
			progressDialog.show();
			ServiceManager.getStoreList(this, new ServiceResponseListener<StoreResponse>() {
				
				@Override
				public void onSuccess(StoreResponse response) {
					if(response!=null){
						progressDialog.dismiss();
						isService=false;
						List<StoreDetails> businessDetailsList=response.getStoreListResponse();
						businessListFragment.setStoreList(businessDetailsList);
						businessListFragment.updateStoreList();
					}
					response.toString();
					
				}
				
				@Override
				public void onFailure(Throwable throwable, String errorResponse) {
					progressDialog.dismiss();
					isService=false;
					
				}
			});
		}
		
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.store_list, menu);
		return true;
	}
	

	private void initListFragment(){
		FragmentManager fm=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction=fm.beginTransaction();
		fragmentTransaction.add(R.id.storeListFragLay, businessListFragment);
		fragmentTransaction.commit();
}
	private void initMapFragment(){
		FragmentManager fm=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction=fm.beginTransaction();
		fragmentTransaction.add(R.id.storeMapFragLay, businessMapFragment);
		fragmentTransaction.commit();
	}

   private void initElements(){
	   storeListLayout=(RelativeLayout)findViewById(R.id.storeListFragLay);
	   storeMapLayout=(RelativeLayout)findViewById(R.id.storeMapFragLay);
	   storeViewButton=(Button)findViewById(R.id.storeViewButton);
	   storeViewButton.setOnClickListener(this);
	   if(!isMapView){
		storeListLayout.setVisibility(View.VISIBLE);
		storeMapLayout.setVisibility(View.GONE);
		}
	   
   }
   
@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.storeViewButton:
		if (! isMapView ) {
			if(!isPlayServiceAvailable){
				int requestCode = 10;
				Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
						playServiceStatus,
						BusinessListActivity.this, requestCode);
				dialog.show();
				return;
			}
			 
		}
		isMapView = isMapView ? false : true;
		
		
		if ( isMapView ) {
			storeViewButton.setText(getString(R.string.list_view));
			storeListLayout.setVisibility(View.INVISIBLE);
			storeMapLayout.setVisibility(View.VISIBLE);

		} else {
			storeViewButton.setText(getString(R.string.map_view));
			storeListLayout.setVisibility(View.VISIBLE);
			storeMapLayout.setVisibility(View.INVISIBLE);
		}
		break;

	default:
		break;
	}
	
}

}
